<?php
/**
 * 评价服务层
 * @author maniac
 */
namespace app\common\service;

use app\common\model\Evaluates AS EvaluatesModel;
use app\common\model\WechatUsers AS WechatUsersModel;
use app\common\model\Orders AS OrdersModel;

class Evaluates extends Base
{
    /**
     * 获取评价列表
     * @param  array  $where 查询条件
     * @param  string $limit 分页条件
     */
    public function evaluates($where, $limit)
    {
        return EvaluatesModel::field('userid,level,level_txt,pics,content,create_time,status,order_id,goods_id,store_id')->fetchSql(false)->append(['nickname', 'headimgurl', 'pics'])->where($where)->order('id DESC')->limit($limit)->select();
    }

    /**
     * 获取评论数量
     */
    public function countevaluates($where)
    {
        return EvaluatesModel::where($where)->count("*");
    }

    /**
     * 添加评价
     * @param array $data 请求的数据
     */
    public function add($data)
    {
//        $wechat = WechatUsersModel::field('nickname')->where('id', $data['userid'])->find();
        $goods = explode(',',$data['goods_id']);//拆分多商品评论
        if(count($goods) > 1){//判断是否为多商品
//            $store = explode('_',$data['store_id']);//商户
//            $index = explode('_',$data['index']);//好评差评
            $level = explode('_',$data['level']);//星级
//            $content = explode('_',$data['content']);//内容
           $pics = explode('_',$data['pics']);//图片
//            $sku_id = explode('_',$data['sku_id']);//规格id
//            $sku_name = explode('_',$data['sku_name']);//规格名称
            foreach ($goods as $k => $v){
                $create[] = [
                    'userid'    => $data['userid'],
//                    'level_txt'     => $index[$k],
                    'level' => $data['level'],
                    'content'   => $data['content'],
                    'goods_id'   => $v,
//                    'store_id'   => $store[$k],
                    'pics'      => $pics[$k],
//                    'sku_id'      => $sku_id[$k],
//                    'sku_name'      => $sku_name[$k],
                    'order_id'   => $data['order_id'],
                ];
                EvaluatesModel::where('id',$v)->setInc('evaluate_nums');
            }
            $info = (new EvaluatesModel)->saveAll($create);
            if ($info) {
                OrdersModel::where('id',$data['orderid'])->update(['is_evaluates'=>1]);
                return 1;
            }

        }else{
            $create = [
                'userid'    => $data['userid'],
//                'level_txt'     => $data['index'],
                'level' => $data['level'],
                'content'   => $data['content'],
                'goods_id'   => $data['goods_id'],
                'store_id'   => $data['store_id'],
                'sku_id'   => $data['sku_id'],
                'sku_name'   => $data['sku_name'],
                'order_id'   => $data['order_id'],
                'pics'      => isset($data['pics']) && !empty($data['pics']) ? $data['pics'] : '',
            ];
            EvaluatesModel::where('id',$data['goods_id'])->setInc('evaluate_nums');
            $info = EvaluatesModel::create($create);


            if ($info->id) {
                OrdersModel::where('id',$data['order_id'])->update(['is_evaluates'=>1]);
                return $info->id;
            }

        }

        return false;
    }
}
